package com.wits.qso.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.wits.platform.service.BaseService;
import com.wits.qso.dao.CourseDao;
import com.wits.qso.dao.CourseRegisterDao;
import com.wits.qso.dao.CourseTeacherViewDao;
import com.wits.qso.dao.StudentDao;
import com.wits.qso.dao.TeacherDao;
import com.wits.qso.entity.CourseRegister;
import com.wits.qso.entity.CourseTeacherView;

@Component
public class CourseRegisterService extends BaseService<CourseRegister, String> {

	/**
	 * 课程注册数据库操作类
	 */
	private CourseRegisterDao courseRegisterDao;
	/**
	 * 课程数据库操作类
	 */
	@Resource
	private CourseDao courseDao;
	/**
	 * 老师数据库操作类
	 */
	@Resource
	private TeacherDao teacherDao;

	/**
	 * 学生数据库操作类
	 */
	@Resource
	private StudentDao studentDao;
	
	/**
	 * 课程老师数据库操作类
	 */
	@Resource
	private CourseTeacherViewDao courseTeacherViewDao;

	@Resource
	public void setCourseRegisterDao(CourseRegisterDao courseRegisterDao) {
		this.baseDao = courseRegisterDao;
		this.courseRegisterDao = courseRegisterDao;
	}

	/**
	 * 根据不同的参数查询课程注册列表
	 * @param param
	 * @return
	 */
	public List<CourseRegister> findCourseRegisters(Map<String, Object> param) {
		return courseRegisterDao.findCourseRegisters(param);
	}
	
	/**
	 * 根据不同的参数查询课程教师视图列表
	 * @param param
	 * @return
	 */
	public List<CourseTeacherView> findCourseTeacherViews(Map<String, Object> param) {
		return courseTeacherViewDao.findCourseTeacherViews(param);
	}
	
	/**
	 * 将单条申请记录状态改为待审（即提交申请动作）
	 * @param id
	 * @return
	 */
	public boolean doApply(String id){
		return updateStatus(id,"AP");
	}
	/**
	 * 将单条申请记录状态改为通过（即同意注册动作）
	 * @param id
	 * @return
	 */
	public boolean doAgree(String id,String message){
		return agreeReject(id,message,"CO");
	}
	/**
	 * 将单条申请记录状态改为拒绝（即拒绝注册动作）
	 * @param id
	 * @return
	 */
	public boolean doReject(String id,String message){
		return agreeReject(id,message,"DL");
	}
	
	public boolean  modifyStatus(String id,String status){
		return updateStatus(id,status);
	}
	
	private boolean updateStatus(String id,String status){
		Map<String ,Object > param=new HashMap<String,Object>();
		param.put("cr_id", id);
		param.put("cr_status", status);
		param.put("date_apply", new Date());
		return courseRegisterDao.modifyFeild(param);
	}
	
	private boolean agreeReject(String id,String message,String status){
		Map<String ,Object > param=new HashMap<String,Object>();
		param.put("cr_id", id);
		param.put("cr_message", message);
		param.put("cr_status", status);
		param.put("date_auth", new Date());
		return courseRegisterDao.modifyFeild(param);
	}
}
